import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;

/**
 * @author zhu
 * @date 2024-03-28
 */
public class MainTest02 {
    public static void copyProperties(Object source,Object target) throws Exception{
        BeanInfo beanInfo = Introspector.getBeanInfo(source.getClass(),Object.class);
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (PropertyDescriptor propertyDescriptor : propertyDescriptors){
            Method getName = propertyDescriptor.getReadMethod();
            Object o = getName.invoke(source);

            PropertyDescriptor targetProperty = new PropertyDescriptor(propertyDescriptor.getName(),target.getClass());
            Method setName = targetProperty.getWriteMethod();
            setName.invoke(target,o);
        }
    }

    public static void main(String[] args) throws Exception {
        Person p1 = new Person();
        p1.setName("zs");
        p1.setAge(18);
        Person p2 = new Person();
        copyProperties(p1,p2);
        System.out.println(p2.getName() + " " + p2.getAge());
    }
}
